//== 个人中心-概览
let PersonalCenterOverview = function () {
    let titleWidth = 320;
    /**
     * 获取标题区域可用宽度
     * @return {number}
     */
    let getTitleWidth = function () {
        let minWidth = 260;
        let width = $('#message-body').width() - 180;
        return Math.max(minWidth, width);
    };
    /**
     * 读消息
     *
     * @param element {object} 消息链接
     */
    let readMessage = function (element) {
        let $link = $(element);
        // 移除未读class
        $link.removeClass('unread');
        // 打开页面
        EApp.openPage($link.text(), $link.data('url'));
    };
    /**
     * 初始化收信
     */
    let initReceive = function () {
        let $status = $('#status');
        // 消息必须是已发送
        $status.val(STATUS_HAS_BEEN_SENT);
        PersonalCenterOverview.messageList = ETool.initDataTable({
            selector: '#message-list',
            url: basePath + '/auth/sys/message/select/receive',
            data: {
                pageSize: 10
            },
            layout: {
                header: false,
                height: 460
            },
            pagination: false,
            columns: [
                {
                    field: 'title',
                    title: '主题',
                    width: titleWidth,
                    template: function (row) {
                        return '\
                        <div class="e-user-card-hor">\
                            <div class="e-user-card-hor-pic">\
                                ' + (EUtil.isNotBlank(row.avatar) ? '<img src="' + basePath + row.avatar + '" alt="photo">' : '<span class="badge badge--unified-info badge--lg badge--bold">' + row.nickname.substr(0, 1) + '</span>') + '  \
                            </div>\
                            <div class="e-user-card-hor-details">\
                                <a href="javascript:;" data-url="' + basePath + '/auth/sys/message/info/' + row.messageId + '/' + row.id + '"' +
                                    'class="e-user-card-hor-name message-link' + (row.readDate ? '' : ' unread') + '">' +
                                (row.important === 1 ? '<span class="badge badge--danger badge--inline badge--unified-danger">重要</span> ' : '') +
                                row.title + '</a>\
                                <span class="e-user-card-hor-email">' + row.nickname + '</span>\
                            </div>\
                        </div>';
                    }
                },
                {
                    field: 'sendDate',
                    title: '类型/时间',
                    width: 100,
                    template: function (row) {
                        let typeDicts = ETool.getSysDictsObjectByDictType('messageType');
                        return '\
                        <div class="e-user-card-hor">\
                            <div class="e-user-card-hor-details">\
                                <a href="#" class="e-user-card-hor-name">' + ETool.getDictElement(row.type, typeDicts) + '</a>\
                                <span class="e-user-card-hor-email" title="' + row.sendDate + '">' + (moment(row.sendDate, 'YYYY-MM-DD HH:mm:ss').fromNow()) + '</span>\
                            </div>\
                        </div>';
                    }
                }
            ]
        });
    };

    /**
     * 初始化待办
     */
    let initTask = function (){
        PersonalCenterOverview.taskList = ETool.initDataTable({
            selector: '#task-list',
            url: basePath + '/auth/activiti/task/select/claimed',
            data: {
                pageSize: 10
            },
            layout: {
                header: false,
                height: 460
            },
            pagination: false,
            columns: [
                {
                    field: 'title',
                    title: '主题',
                    width: titleWidth,
                    template: function (row) {
                        return '\
                        <div class="e-user-card-hor">\
                            <div class="e-user-card-hor-pic">\
                                ' + (EUtil.isNotBlank(row.avatar) ? '<img src="' + basePath + row.avatar + '" alt="photo">' : '<span class="badge badge--unified-info badge--lg badge--bold">' + row.applyUser.substr(0, 1) + '</span>') + '  \
                            </div>\
                            <div class="e-user-card-hor-details">\
                                <a href="javascript:;" class="e-user-card-hor-name message-link" data-url="">' +
                                    row.businessTitle + '\
                                </a>\
                                <span class="e-user-card-hor-email">' + row.applyUser + '</span>\
                            </div>\
                        </div>';
                    }
                },
                {
                    field: 'sendDate',
                    title: '类型/时间',
                    width: 100,
                    template: function (row) {
                        return '\
                        <div class="e-user-card-hor">\
                            <div class="e-user-card-hor-details">\
                                <a href="#" class="e-user-card-hor-name">NO：' + row.processInstanceId + '</a>\
                                <span class="e-user-card-hor-email" title="' + row.processDefinitionName + '">' + row.processDefinitionName + '</span>\
                            </div>\
                        </div>';
                    }
                }
            ]
        });
    }
    /**
     * 绑定事件
     */
    let bind = function () {
        $('#message-list').on('click', '[data-url]', function () {
            readMessage(this);
        });    
    };
    return {
        //== 初始化页面
        init: function () {
            titleWidth = getTitleWidth();
            initReceive();
            initTask();
            bind();
        }
    };
}();
/**
 * 标签页激活回调
 *
 * @return {boolean} true/false
 */
ETab.needSubmitForm = function () {
    PersonalCenterOverview.messageList.reload();
    PersonalCenterOverview.taskList.reload();
    return false;
}